﻿'******************************* File Header *********************************
'* File Name:    WmiConnectionStringBuilderTest.vb
'* Project:      WmiConnection.Test
'* Copyright (c) Freddy Kuehne
'*
'* Unit tests for WmiConnectionStringBuilder.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************
Imports System.Text
Imports System.Data.Common

<TestClass()>
Public Class WmiConnectionStringBuilderTest
#Region "Tests"
  <TestMethod()>
  Public Sub Constructor1Test()
    Dim target As New WmiConnectionStringBuilder()

    Assert.IsTrue(String.IsNullOrEmpty(target.Server))
    Assert.IsTrue(String.IsNullOrEmpty(target.Path))
    Assert.AreEqual(target.Path, target.Database)
    Assert.IsTrue(String.IsNullOrEmpty(target.UserID))
    Assert.IsTrue(String.IsNullOrEmpty(target.Password))
  End Sub

  <TestMethod()>
  Public Sub Constructor2Test()
    Dim target As New WmiConnectionStringBuilder("utserver", "\unittest\ns", "unittest", "utpwd")

    Assert.AreEqual("utserver", target.Server)
    Assert.AreEqual("\unittest\ns", target.Path)
    Assert.AreEqual("unittest", target.UserID)
    Assert.AreEqual("utpwd", target.Password)
  End Sub

  <TestMethod()>
  Public Sub Constructor3Test()
    Dim target As New WmiConnectionStringBuilder("server=utserver;path=\unittest\ns;userid=unittest;password=utpwd")

    Assert.AreEqual("utserver", target.Server)
    Assert.AreEqual("\unittest\ns", target.Path)
    Assert.AreEqual(target.Path, target.Database)
    Assert.AreEqual("unittest", target.UserID)
    Assert.AreEqual("utpwd", target.Password)
  End Sub

  <TestMethod()>
  Public Sub HierarchyTest()
    Dim target As New WmiConnectionStringBuilder
    Assert.IsInstanceOfType(target, GetType(DbConnectionStringBuilder))
  End Sub

  <TestMethod()>
  Public Sub ServerTest()
    Dim target As New WmiConnectionStringBuilder

    Assert.IsTrue(String.IsNullOrEmpty(target.Server))
    target.Server = "utserver"
    Assert.AreEqual("utserver", target.Server)
    target.Server = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(target.Server))
    target.Server = "UTSERVER"
    Assert.AreEqual("UTSERVER", target.Server)

    ' item and caseinsensitivity
    target.Item("server") = "itemserver"
    Assert.AreEqual("itemserver", target.Server)
    target.Item("Server") = "itemserver2"
    Assert.AreEqual("itemserver2", target.Server)
  End Sub

  <TestMethod()>
  Public Sub PathTest()
    Dim target As New WmiConnectionStringBuilder

    Assert.IsTrue(String.IsNullOrEmpty(target.Path))
    target.Path = "\unittest\ns"
    Assert.AreEqual("\unittest\ns", target.Path)
    target.Path = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(target.Path))
    target.Path = "\UNITTEST\NS"
    Assert.AreEqual("\UNITTEST\NS", target.Path)

    ' item and caseinsensitivity
    target.Item("path") = "itempath"
    Assert.AreEqual("itempath", target.Path)
    target.Item("path") = "itempath2"
    Assert.AreEqual("itempath2", target.Path)
  End Sub

  <TestMethod()>
  Public Sub DatabaseTest()
    Dim target As New WmiConnectionStringBuilder

    Assert.IsTrue(String.IsNullOrEmpty(target.Database))
    target.Database = "\unittest\ns"
    Assert.AreEqual("\unittest\ns", target.Database)
    target.Database = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(target.Database))
    target.Database = "\UNITTEST\NS"
    Assert.AreEqual("\UNITTEST\NS", target.Database)

    ' item and caseinsensitivity
    target.Item("database") = "itempath"
    Assert.AreEqual("itempath", target.Database)
    target.Item("database") = "itempath2"
    Assert.AreEqual("itempath2", target.Database)
  End Sub

  <TestMethod()>
  Public Sub UserIDTest()
    Dim target As New WmiConnectionStringBuilder

    Assert.IsTrue(String.IsNullOrEmpty(target.UserID))
    target.UserID = "unittest"
    Assert.AreEqual("unittest", target.UserID)
    target.UserID = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(target.UserID))
    target.UserID = "UNITTEST"
    Assert.AreEqual("UNITTEST", target.UserID)

    ' item and caseinsensitivity
    target.Item("userid") = "itemuser"
    Assert.AreEqual("itemuser", target.UserID)
    target.Item("userid") = "itemuser2"
    Assert.AreEqual("itemuser2", target.UserID)
  End Sub

  <TestMethod()>
  Public Sub PasswordTest()
    Dim target As New WmiConnectionStringBuilder

    Assert.IsTrue(String.IsNullOrEmpty(target.Password))
    target.Password = "utpwd"
    Assert.AreEqual("utpwd", target.Password)
    target.Password = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(target.Password))
    target.Password = "UTPWD"
    Assert.AreEqual("UTPWD", target.Password)

    ' item and caseinsensitivity
    target.Item("password") = "itempwd"
    Assert.AreEqual("itempwd", target.Password)
    target.Item("password") = "itempwd2"
    Assert.AreEqual("itempwd2", target.Password)
  End Sub

  <TestMethod()>
  Public Sub ItemTest()
    Dim target As New WmiConnectionStringBuilder

    target.Item("server") = "utserver"
    Assert.AreEqual("utserver", target.Item("server"))
    Assert.AreEqual("utserver", target.Item("SERVER"))
    target.Item("server") = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(CStr(target.Item("server"))))
  End Sub

  <TestMethod()>
  Public Sub ConnectionStringTest()
    Dim target As New WmiConnectionStringBuilder

    target.Server = "utserver"
    Assert.AreEqual("Server=utserver;path=;userid=;password=", target.ConnectionString, True)
    target.Path = "\unittest\ns"
    Assert.AreEqual("Server=utserver;Path=\unittest\ns;userid=;password=", target.ConnectionString, True)
    target.UserID = "unittest"
    Assert.AreEqual("Server=utserver;Path=\unittest\ns;UserID=unittest;password=", target.ConnectionString, True)
    target.Password = "utpwd"
    Assert.AreEqual("Server=utserver;Path=\unittest\ns;UserID=unittest;Password=utpwd", target.ConnectionString, True)

    target.ConnectionString = Nothing
    Assert.IsTrue(String.IsNullOrEmpty(target.Server))
    Assert.IsTrue(String.IsNullOrEmpty(target.Path))
    Assert.IsTrue(String.IsNullOrEmpty(target.UserID))
    Assert.IsTrue(String.IsNullOrEmpty(target.Password))

    target.ConnectionString = "Server=utserver;Path=\unittest\ns;UserID=unittest;Password=utpwd;"
    Assert.AreEqual("utserver", target.Server)
    Assert.AreEqual("\unittest\ns", target.Path)
    Assert.AreEqual("unittest", target.UserID)
    Assert.AreEqual("utpwd", target.Password)

    target.ConnectionString = Nothing
    target.ConnectionString = "SERVER =  utserver ; PATH =  \unittest\ns ; USERID =  unittest ; PASSWORD =  utpwd;  "
    Assert.AreEqual("utserver", target.Server)
    Assert.AreEqual("\unittest\ns", target.Path)
    Assert.AreEqual("unittest", target.UserID)
    Assert.AreEqual("utpwd", target.Password)
  End Sub

  <TestMethod()>
  Public Sub Security1Test()
    Dim target As New WmiConnectionStringBuilder

    target.ConnectionString = "Server=""utserver;Path=\unittest\ns;"";"
    Assert.AreEqual("utserver;Path=\unittest\ns;", target.Server)
    Assert.IsTrue(String.IsNullOrEmpty(target.Path))

    target.ConnectionString = "Server="";"";"
    Assert.AreEqual(";", target.Server)

    target.ConnectionString = "Server=""'"";"
    Assert.AreEqual("'", target.Server)

    target.ConnectionString = "Server='""';"
    Assert.AreEqual("""", target.Server)
  End Sub

  <TestMethod()>
  Public Sub Security2Test()
    Dim target As New WmiConnectionStringBuilder

    target.Server = ";"
    Assert.AreEqual("server="";"";path=;userid=;password=", target.ConnectionString, True)

    target.Server = "'"
    Assert.AreEqual("server=""'"";path=;userid=;password=", target.ConnectionString, True)

    target.Server = """"
    Assert.AreEqual("server='""';path=;userid=;password=", target.ConnectionString, True)
  End Sub
#End Region
End Class
